/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.softbook.view;

import br.com.softbook.controler.FornecedorControler;
import br.com.softbook.bean.Cadastrofornecedores;
import java.util.List;
import java.util.Vector;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Adriano
 */
public class formFornecedorConsulta extends javax.swing.JDialog {

    /**
     * Creates new form formFornecedorConsulta
     */
    private DefaultTableModel model = new DefaultTableModel();
    private FornecedorControler fornecedorControler = new FornecedorControler();
    private Cadastrofornecedores forn = null;

    public formFornecedorConsulta(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        //Look And Feell
        try {
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
            SwingUtilities.updateComponentTreeUI(this);
        } catch (Exception e) {
            e.printStackTrace();
        }
        initComponents();
        this.setLocationRelativeTo(this);
        carregaCabecalho();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jTFNomeconsulta = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTblDados = new javax.swing.JTable();
        jBOkConsulta = new javax.swing.JButton();
        jBCancelarConsulta = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel1.setText("Nome");

        jTFNomeconsulta.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                jTFNomeconsultaKeyReleased(evt);
            }
        });

        jTblDados.setModel(model);
        jScrollPane1.setViewportView(jTblDados);

        jBOkConsulta.setText("OK");
        jBOkConsulta.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBOkConsultaActionPerformed(evt);
            }
        });

        jBCancelarConsulta.setText("Cancelar");
        jBCancelarConsulta.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBCancelarConsultaActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jTFNomeconsulta)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(jBOkConsulta, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(jBCancelarConsulta))
                                .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 375, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(0, 5, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTFNomeconsulta, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jBOkConsulta)
                    .addComponent(jBCancelarConsulta))
                .addContainerGap(21, Short.MAX_VALUE))
        );

        setSize(new java.awt.Dimension(416, 287));
        setLocationRelativeTo(null);
    }// </editor-fold>//GEN-END:initComponents

    private void jBCancelarConsultaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBCancelarConsultaActionPerformed
        forn = null;
        this.dispose();
    }//GEN-LAST:event_jBCancelarConsultaActionPerformed

    private void jBOkConsultaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBOkConsultaActionPerformed
        selecionarProduto();
        this.dispose();
    }//GEN-LAST:event_jBOkConsultaActionPerformed

    private void jTFNomeconsultaKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTFNomeconsultaKeyReleased
        String nome = jTFNomeconsulta.getText();
        carregaDados(nome);
    }//GEN-LAST:event_jTFNomeconsultaKeyReleased

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(formFornecedorConsulta.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(formFornecedorConsulta.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(formFornecedorConsulta.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(formFornecedorConsulta.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the dialog */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                formFornecedorConsulta dialog = new formFornecedorConsulta(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jBCancelarConsulta;
    private javax.swing.JButton jBOkConsulta;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField jTFNomeconsulta;
    private javax.swing.JTable jTblDados;
    // End of variables declaration//GEN-END:variables
   private void carregaCabecalho() {
        Vector<String> v = new Vector<String>();
        v.add("Codigo");
        v.add("Nome");
        model.setDataVector(null, v);
        carregaTudo();
    }

    private void carregaDados(String nome) {
        int cont = model.getRowCount();
        for (int i = 0; i < cont; i++) {
            model.removeRow(0);
        }

        List<Cadastrofornecedores> list = fornecedorControler.buscarPorNome(nome);
        for (Cadastrofornecedores fornecedor : list) {
            model.addRow(new Object[]{
                fornecedor.getIdcadastrofornecedores(),
                fornecedor.getFornecedoresnome(),});
        }
    }

    private void carregaTudo() {
        int cont = model.getRowCount();
        for (int i = 0; i < cont; i++) {
            model.removeRow(0);
        }
        List<Cadastrofornecedores> list = fornecedorControler.buscarTodos();
        for (Cadastrofornecedores f : list) {
            model.addRow(new Object[]{
                f.getIdcadastrofornecedores(),
                f.getFornecedoresnome()
            });
        }
    }

    private void selecionarProduto() {
        Integer codFor = (Integer) model.getValueAt(jTblDados.getSelectedRow(), (0));
        forn = fornecedorControler.buscar(codFor);
    }

    public Cadastrofornecedores getFornecedor() {
        return forn;
    }
}
